package com.wyzz.hr.action;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.base.wf.overtimerequest.OrWfApproverAction;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.ILeavetypeBO;
import com.wyzz.hr.bo.IOvertimerequestBo;
import com.wyzz.hr.exportfile.ExportTiaoxiu;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.pojo.Overtimerequest;
import com.wyzz.hr.pojo.TiaoxiuBean;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.impl.PropertiesFileConfigManager;
import com.wyzz.hr.vo.ExaminSearchBean;
import com.wyzz.hr.vo.OvertimeFormBean;

public class AllOvertimeSearchAction extends OrWfApproverAction {

	public AllOvertimeSearchAction() {
		searchOrExport = null;
	}

	public String execute() throws Exception {
		srcAction = "allOverTimeSearch";
		initOTLists();
		if (page == null) {
			page = new Pager();
			page.setOrder("");
		}
		if (es_Bean == null)
			es_Bean = new ExaminSearchBean();
		if ("tiaoxiuExport".equals(searchOrExport))
			return tiaoxiuExport();
		IOvertimerequestBo or_BO = (IOvertimerequestBo) getBean("overtimerequestBO");
		String auth = checkActionAuth("allOvertimeSearch", "execute");
		DetachedCriteria dc = DetachedCriteria.forClass(Overtimerequest.class);
		if (auth.equals("SUB"))
			addSubDC(dc, "emp", 4);
		or_BO.examinSearch(es_Bean, dc, page);
		if (!StringUtils.isEmpty(infoMeg))
			addSuccessInfo(infoMeg);
		return "success";
	}

	private String tiaoxiuExport() {
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		int statusSet_approved[] = { 15, 16 };
		List empList = empBo.getAllEmpBasicInfo();
		ILeavetypeBO lt_BO = (ILeavetypeBO) getBean("leavetypeBO");
		Leavetype lt = lt_BO.getLeavetypeByCat(Integer.valueOf(11));
		System.out.println(lt);
		IOvertimerequestBo or_Bo = (IOvertimerequestBo) getBean("overtimerequestBO");
		List tiaoxiuOrList = or_Bo
				.getEmpTiaoxiuOrList(null, statusSet_approved);
		Map empTiaoxiuOrMap = tiaoxiuOrListToMap(tiaoxiuOrList);
		Date now = new Date();
		List tiaoxiuBeanList = new ArrayList();
		IDepartmentBO deptBo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		deptBo.setListDeptLevels(empList, "empDeptNo", "empBranch",
				"empDeptNo", new String[0]);
		Iterator it = empList.iterator();
		do {
			if (!it.hasNext())
				break;
			Employee emp = (Employee) it.next();
			if (empTiaoxiuOrMap.containsKey(emp.getId())) {
				TiaoxiuBean tiaoxiuBean = new TiaoxiuBean();
				tiaoxiuBean.setEmp(emp);
				String tiaoxiuData[][] = or_Bo.getEmpTiaoxiu(lt, now,
						(List) empTiaoxiuOrMap.get(emp.getId()));
				boolean flag = false;
				for (int i = 0; i < tiaoxiuData.length; i++)
					if (!tiaoxiuData[i][1].split("-")[1].equals("0.0")
							|| !tiaoxiuData[i][2].split("-")[1].equals("0.0")
							|| !tiaoxiuData[i][3].split("-")[1].equals("0.0"))
						flag = true;

				if (flag) {
					tiaoxiuBean.setResult(tiaoxiuData);
					tiaoxiuBeanList.add(tiaoxiuBean);
				}
			}
		} while (true);
		return exportData(tiaoxiuBeanList);
	}

	private Map tiaoxiuOrListToMap(List tiaoxiuOrList) {
		if (tiaoxiuOrList == null)
			return null;
		Map empTiaoxiuOrMap = new HashMap();
		Overtimerequest otr;
		List list;
		for (Iterator i$ = tiaoxiuOrList.iterator(); i$.hasNext(); empTiaoxiuOrMap
				.put(otr.getOrEmpNo().getId(), list)) {
			otr = (Overtimerequest) i$.next();
			if (!empTiaoxiuOrMap.containsKey(otr.getOrEmpNo().getId()))
				empTiaoxiuOrMap.put(otr.getOrEmpNo().getId(), new ArrayList());
			list = (List) empTiaoxiuOrMap.get(otr.getOrEmpNo().getId());
			list.add(otr);
		}

		return empTiaoxiuOrMap;
	}

	public String exportData(List tiaoxiuBeanList) {
		String downFileName = null;
		String filePath = FileOperate.getFileHomePath();
		String xslFileName = (new StringBuilder()).append("export-").append(
				FileOperate.buildFileName()).append(
				PropertiesFileConfigManager.getInstance().getProperty(
						"sys.saveDir.type").trim()).toString();
		filePath = (new StringBuilder()).append(filePath).append(xslFileName)
				.toString();
		try {
			ExportTiaoxiu exportTx = new ExportTiaoxiu(tiaoxiuBeanList,
					"\u5BFC\u51FA");
			exportTx.export(filePath);
			downFileName = "TiaoxiuLeaveBalance.xls";
			File file = new File(filePath);
			FileInputStream fileInputStream = new FileInputStream(file);
			setDocStream(fileInputStream);
			setContentDisposition((new StringBuilder()).append("filename=\"")
					.append(downFileName).append("\"").toString());
			setServerFileName(filePath);
			clearErrorsAndMessages();
		} catch (Exception e) {
			addActionError("\u6570\u636E\u5BFC\u51FA\u5931\u8D25\uFF01");
			return "success";
		}
		return "download";
	}

	public String hrOtConfirmWithModify() throws Exception {
		if (of_Bean == null)
			return "error";
		String auth = super.authorityCondition;
		if (!"HR".equals(auth)) {
			addErrorInfo("\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF01");
			return "noauth";
		} else {
			return wfObjOpSingle("hr-confirm", of_Bean, orIdUp);
		}
	}

	public void validate() {
		validateDate(es_Bean);
		validateOTStatus(es_Bean);
	}

	/**
	 * @deprecated Method get24Point is deprecated
	 */

	public String get24Point(Date date) {
		if (date == null)
			date = new Date();
		String result = null;
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		if ("00:00".equals(format.format(date)))
			result = "24:00";
		else
			result = format.format(date);
		return result;
	}

	public ExaminSearchBean getEs_Bean() {
		return es_Bean;
	}

	public void setEs_Bean(ExaminSearchBean es_Bean) {
		this.es_Bean = es_Bean;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getSrcAction() {
		return srcAction;
	}

	public void setSrcAction(String srcAction) {
		this.srcAction = srcAction;
	}

	public OvertimeFormBean getOf_Bean() {
		return of_Bean;
	}

	public void setOf_Bean(OvertimeFormBean ofBean) {
		of_Bean = ofBean;
	}

	public String getOrIdUp() {
		return orIdUp;
	}

	public void setOrIdUp(String orIdUp) {
		this.orIdUp = orIdUp;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	private static final long serialVersionUID = 1L;
	private ExaminSearchBean es_Bean;
	private Pager page;
	private String srcAction;
	private OvertimeFormBean of_Bean;
	private String orIdUp;
	private String searchOrExport;
}
